package com.zmn.plat.admin.controller.service.itemdetail;

import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.plat.dubbo.utils.DubboConsts;
import com.zmn.plat.model.entity.service.itemdetail.ItemDetail;
import com.zmn.plat.services.interfaces.service.itemdetail.ItemDetailService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * 项目明细-计价器3.0基础修改Controller
 *
 * @author guoqiao
 * @since 2021/11/19 15:02
 */
@Slf4j
@RestController
@RequestMapping("/service/itemdetail")
public class ItemDetailModifyController extends ServiceItemDetailBaseController{

    private ItemDetailService itemDetailService;

    /**
     * 描述: 保存
     *
     * @author guoqiao
     * @since 2021/11/19 15:02
     */
    @RequestMapping("/addEdit")
    public ResponseDTO addEdit(HttpServletRequest request, @RequestBody ItemDetail itemDetail) {

        Date now = DateUtil.getNow();
        int itemId = itemDetail.getItemId();
        try {
            itemDetail.setUpdater(getMcStaffRealName());
            itemDetail.setUpdateTime(now);
            if (NumberUtil.isNullOrZero(itemId)) {
                itemDetail.setCreater(getMcStaffRealName());
                itemDetail.setCreateTime(now);
                itemDetailService.insert(itemDetail);
            } else {
                itemDetailService.updateByKey(itemDetail);
            }
            return ResponseDTO.success("操作成功！");
        } catch (Exception e) {
            log.error("新增/修改失败,Exception{},msg{}", e, e.getMessage());
            return ResponseDTO.fail("操作失败！");
        }
    }
    /**
     * 描述: 删除
     * @param itemId 主键
     *
     * @author guoqiao
     * @since 2021/11/19 15:02
     */
    @RequestMapping(value = "/delete")
    public ResponseDTO delete(@RequestParam(value = "itemId") Integer itemId) {

        try {
            if (NumberUtil.isNullOrZero(itemId)) {
                return ResponseDTO.fail("请选择要删除的数据！");
            }
            itemDetailService.deleteByKey(itemId);
            return ResponseDTO.success("删除成功！");
        } catch (Exception e) {
            log.error("删除失败,Exception{},msg{}", e, e.getMessage());
            return ResponseDTO.fail("操作失败！");
        }
    }

}
